import {observable, action, toJS} from 'mobx';
import {WeaTableNew, WeaForm} from 'comsMobx';
import {WeaLocaleProvider} from "ecCom";
import {message, Modal} from "antd";
import _ from 'lodash';
import * as contractManagementApi from '../api/ContractManagementApi';
import {indexOf, isBlank, parseIntValue} from '../util/CommonUtil';
import LanguageConstant from '../constant/LanguageConstant';

const {getLabel} = WeaLocaleProvider;

class BookmarkMatchingStore {

    /**
     * 是否有操作页面的权限
     *
     * @type {boolean}
     */
    @observable authorized = true;

    /**
     * 加载状态
     */
    @observable loading = false;

    /**
     * 下拉选择框的选项
     */
    @observable selectOptions = [];

    /**
     * 书签匹配数据
     *
     * @type {Array}
     */
    @observable bookmarkMatchingDatas = [];

    /**
     * 模板流程关系id
     *
     * @type {number}
     */
    @observable workflowMouldRelationalId = -1;

    /**
     * 手动修改过
     */
    @observable manuallyModifed = false;

    @action
    init = (workflowMouldRelationalId) => {
        this.resetBookmarkMatchingDatas();
        this.workflowMouldRelationalId = workflowMouldRelationalId;
    }

    /**
     * 重置数据
     */
    @action
    resetBookmarkMatchingDatas = () => {
        this.authorized = true;
        this.loading = false;
        this.selectOptions = [];
        this.bookmarkMatchingDatas = [];
        this.workflowMouldRelationalId = -1;
        this.manuallyModifed = false;
    }

    /**
     * 书签匹配表字段变化
     *
     * @param field
     * @param value
     * @param record
     */
    @action
    changeBookMarkTableField = (id, field, value, record) => {
        if (field != undefined) {
            const bookmarkMatchingDatas = toJS(this.bookmarkMatchingDatas);
            for (let i = 0; i < bookmarkMatchingDatas.length; i++) {
                let item = bookmarkMatchingDatas[i];
                if (item && item.id == id && value != undefined) {
                    // 流程字段变化时，清空显示设置选项
                    if (field == "fieldId" && item[field] != value) {
                        item.showType = "";
                        item.percentagePrecision = 2;
                        item.floatPrecision = 2;
                    }
                    item[field] = value;
                    this.manuallyModifed = true;
                    break;
                }
            }
            this.bookmarkMatchingDatas = bookmarkMatchingDatas;
        }
    }

    /**
     * 保存书签匹配数据
     *
     * @param params 参数
     * @param successCallBack 保存成功回调
     */
    @action
    saveBookmarkMatching = (params = {}, successCallback) => {
        const workflowMouldRelationalId = toJS(this.workflowMouldRelationalId);
        const bookmarkMatchingDatas = JSON.stringify(toJS(this.bookmarkMatchingDatas));
        const method = "edit";
        params = {...params, method, workflowMouldRelationalId, bookmarkMatchingDatas}
        contractManagementApi.saveBookmarkMatchingDatas(params).then(res => {
            const {code} = res;
            if (code == 200) {
                message.success(getLabel(LanguageConstant.SAVE_SUCCESS, "保存成功"));
                if (_.isFunction(successCallback)) {
                    successCallback();
                }
            } else {
                message.error(getLabel(LanguageConstant.SAVE_FAIL, "保存失败"));
            }
        });
    }

    /**
     * 获取书签匹配表的数据
     *
     * @param params
     */
    @action
    getBookmarkMatchingTableDatas = (params = {}) => {
        const workflowMouldRelationalId = toJS(this.workflowMouldRelationalId);
        params = {...params, workflowMouldRelationalId}
        contractManagementApi.getWorkflowMouldBookmarkMatchingTable(params).then(res => {
            const {code, data} = res;
            if (code == 200) {
                const {selectOptions, bookmarkMatchingDatas} = data;
                this.selectOptions = selectOptions;
                this.bookmarkMatchingDatas = bookmarkMatchingDatas;
            }
        });
    }

}

export default BookmarkMatchingStore;